package com.ziwei.material.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziwei.material.domain.po.SdsMaterialTask;
import com.ziwei.material.domain.vo.ResultVo;
import com.ziwei.material.mapper.MaterialTaskMapper;
import com.ziwei.material.service.MaterialTaskImportService;
import com.ziwei.material.service.MaterialTaskService;
import com.ziwei.material.util.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Random;

/**
 * @author: jl_zhang
 * @since: 2024/11/14 14:18
 **/

//素材导入服务实现类
@Service
public class MaterialTaskServiceImpl extends ServiceImpl<MaterialTaskMapper, SdsMaterialTask> implements MaterialTaskService {

    @Resource
    private MaterialTaskImportService materialTaskImportService;

    @Resource
    private MaterialTaskMapper materialTaskMapper;



    @Override
    public ResultVo<Void> importExcel(MultipartFile excel, Long userId) {
        if (excel.isEmpty()){
            return ResultVo.fail("文件不得为空!");
        }

        if (!FileUtils.isValidExcelOrCsv(excel)){
            return ResultVo.fail("请上传xls, xlsx, csv格式的文件");
        }

        // 校验是否是正常的excel文件
        if(!FileUtils.isRealExcel(excel)){
            return ResultVo.fail("文件内容异常，请检查后重新尝试");
        }

        if (!FileUtils.isLengthValid(excel)){
            return ResultVo.fail("一次导入的url条数不能超过1000条");
        }


        //开始生成任务

        //1.生成批次号, 格式为: IMTyyyymmddhhmmss+三位随机数
        String batchNo = getMyBatchNo();
        SdsMaterialTask materialTask = new SdsMaterialTask();
        materialTask.setBatchNo(batchNo);
        materialTask.setImportNum(0);
        materialTask.setDownloadNum(0);
        materialTask.setAnalyseNum(0);
        materialTask.setCreateTime(LocalDateTime.now());
        materialTask.setTaskStatus(0);   //设置任务状态  0为导入中
        materialTask.setUserId(userId);

        save(materialTask);

        Long taskId = materialTask.getId();

        //开始执行异步导入
        materialTaskImportService.importExecute(excel, userId, taskId);


        return ResultVo.success("操作开始", null);
    }

    //更新任务表导入字段和状态
    @Override
    public void updateTaskImportCondition(int num, Long taskId) {
        int taskStatus = 1;
        materialTaskMapper.updateTaskImportCondition(num, taskStatus,taskId);
    }

    //更新任务表状态(全部导入失败)
    @Override
    public void updateTaskFailImportCondition(Long taskId) {
        int taskStatus = 3;
        materialTaskMapper.updateTaskFailImportCondition(taskStatus, taskId);
    }

    @Override
    public void updateTaskDownloadCondition(int downloadNum, Long taskId) {
        int taskStatus = 2;
        materialTaskMapper.updateTaskDownloadCondition(downloadNum, taskStatus, taskId);
    }


    //更新任务表状态(全部下载失败)
    @Override
    public void updateTaskAllFailDownloadCondition(Long taskId) {
        int taskStatus = 3;
        materialTaskMapper.updateTaskAllFailDownloadCondition(taskStatus, taskId);
    }

    public static String getMyBatchNo(){
        // 获取当前时间, 格式为yyyymmddhhmmss
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

        String formattedDate = sdf.format(new Date());

        //生成三位随机数
        Random random = new Random();
        int randomNum = random.nextInt(1000);  //生成0-999的随机数

        String randomStr = String.format("%03d", randomNum);

        return "IMT" + formattedDate + randomStr;

    }
}
